package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotAnnualInspectionBo;
import org.dromara.iot.domain.vo.IotAnnualInspectionVo;
import org.dromara.iot.service.IIotAnnualInspectionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯年检管理
 *
 * @author lsl
 * @date 2024-06-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/annualInspection")
public class IotAnnualInspectionController extends BaseController {

    private final IIotAnnualInspectionService iotAnnualInspectionService;

    /**
     * 查询电梯年检管理列表
     */
    @SaCheckPermission("iot:annualInspection:list")
    @GetMapping("/list")
    public TableDataInfo<IotAnnualInspectionVo> list(IotAnnualInspectionBo bo, PageQuery pageQuery) {
        return iotAnnualInspectionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯年检管理列表
     */
    @SaCheckPermission("iot:annualInspection:export")
    @Log(title = "电梯年检管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotAnnualInspectionBo bo, HttpServletResponse response) {
        List<IotAnnualInspectionVo> list = iotAnnualInspectionService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯年检管理", IotAnnualInspectionVo.class, response);
    }

    /**
     * 获取电梯年检管理详细信息
     *
     * @param inspectionId 主键
     */
    @SaCheckPermission("iot:annualInspection:query")
    @GetMapping("/{inspectionId}")
    public R<IotAnnualInspectionVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long inspectionId) {
        return R.ok(iotAnnualInspectionService.queryById(inspectionId));
    }

    /**
     * 新增电梯年检管理
     */
    @SaCheckPermission("iot:annualInspection:add")
    @Log(title = "电梯年检管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotAnnualInspectionBo bo) {
        return toAjax(iotAnnualInspectionService.insertByBo(bo));
    }

    /**
     * 修改电梯年检管理
     */
    @SaCheckPermission("iot:annualInspection:edit")
    @Log(title = "电梯年检管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotAnnualInspectionBo bo) {
        return toAjax(iotAnnualInspectionService.updateByBo(bo));
    }

    /**
     * 删除电梯年检管理
     *
     * @param inspectionIds 主键串
     */
    @SaCheckPermission("iot:annualInspection:remove")
    @Log(title = "电梯年检管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inspectionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] inspectionIds) {
        return toAjax(iotAnnualInspectionService.deleteWithValidByIds(List.of(inspectionIds), true));
    }

    /**
     * 启动电梯年检接口
     *
     * @param inspectionId
     */
    @Log(title = "电梯年检管理", businessType = BusinessType.OTHER)
    @RepeatSubmit()
    @PutMapping("/start/{inspectionId}")
    public R<Void> start(@NotNull(message = "主键不能为空") @PathVariable Long inspectionId) {
        return toAjax(iotAnnualInspectionService.startAnnualInspection(inspectionId));
    }


    /**
     * 修改电梯年检管理
     */
    @SaCheckPermission("iot:annualInspection:edit")
    @Log(title = "电梯年检管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/confirmAnnualInspection")
    public R<Void> confirmAnnualInspection(@RequestBody IotAnnualInspectionBo bo) {
        return toAjax(iotAnnualInspectionService.confirmAnnualInspection(bo));
    }
}
